import { _decorator, Button, Component, Node, Sprite, SpriteFrame, v3, Widget } from 'cc';
import { Tools } from '../game/Tools';
const { ccclass, property } = _decorator;

@ccclass('ImgBooks')
export class ImgBooks extends Component {

    @property(Node)
    xq: Node = null;

    @property(Node)
    fish_content: Node = null;

    min_fish_page: Node = null;
    max_fish_page: Node = null;

    min_fish_items: Node[] = [];
    max_fish_items: Node[] = [];


    @property([SpriteFrame])
    min_icons: SpriteFrame[] = [];
    @property([SpriteFrame])
    max_icons: SpriteFrame[] = [];

    start() {
        let unlock = [0, 1, 3]//模拟解锁鱼
        this.set_unlock(unlock)
    }

    set_unlock(unlock: number[]) {
        this.min_fish_page = this.fish_content.getChildByName("img_book_page");

        for (let i = 0; i < this.min_fish_page.children.length; i++) {
            const element = this.min_fish_page.children[i];
            this.min_fish_items.push(element)
        }


        for (let i = 0; i < unlock.length; i++) {
            const u = unlock[i];

            const element = this.min_fish_items[u]

            element.getChildByName("img").getComponent(Sprite).spriteFrame = this.min_icons[u]
            element.getChildByName("mask").active = false;
            //element.addComponent(Button);
            element.on(Button.EventType.CLICK, () => { this.on_xq_item(u, this.min_icons[u]) }, this)
        }

        //--------------------稀有
        this.max_fish_page = this.fish_content.getChildByName("img_book_page_max");

    }

    on_open() {
        Tools.updateWidget(this.node);

    }

    on_close_books() {
        this.node.getComponent(Widget).enabled = false;
        this.node.setPosition(v3(-1900, 0, 0));
    }

    on_tag_btn(e, str: string) {
        console.log("点击：", str)
        if (str == "0") {
            this.min_fish_page.active = true;
            this.max_fish_page.active = false;
        }
        if (str == "1") {
            this.max_fish_page.active = true;
            this.min_fish_page.active = false;
        }
    }

    on_close_xq() {
        this.xq.active = false;
    }

    /** 动态绑定按钮 */
    on_xq_item(index: number, sprf: SpriteFrame) {
        console.log("显示详情", index)
        this.xq.active = true;

        const spr = this.xq.getChildByName("page").getChildByName("img").getComponent(Sprite)
        spr.spriteFrame = sprf
    }
}


